;递归版本
(define (product term a next b)
  (if (> a b)
    1
    (* (term a) (product term (next a) next b))))

;迭代版本
(define (product term a next b)
  (define (product-iter a result)
    (if (> a b)
      result
      (product-iter (next a) (* result (term a)))))
  (product-iter a 1))

(define (identity x) x) 

(define (next x) (+ x 1)) 

(define (factorial n) 
  (product identity 1 next n)) 

(define (pi-term n) 
  (if (even? n) 
    (/ (+ n 2) (+ n 1)) 
    (/ (+ n 1) (+ n 2)))) 
